/*
 * Created: 2022-08-25 20:37:17
 * Author : 涛涛涛
 */

import Taro from '@tarojs/taro';
import { px_2_unit, unit_2_px } from './unitConversion';

const getDomInfoRun = async (domName: string) => {
    const query = Taro.createSelectorQuery();
    let domInfo: any = await new Promise((resolve) => {
        // 为了兼容小程序,取得数据队列的时间,还需要再进行一个队列延迟
        setTimeout(() => {
            query
                .select(domName)
                .fields(
                    {
                        id: true,
                        dataset: true,
                        rect: true,
                        size: true,
                        scrollOffset: true,
                    },
                    (data) => {
                        resolve(data as any);
                    }
                )
                .exec();
        }, 0);
    });
    if (domInfo) {
        delete domInfo['dataset'];
        delete domInfo['id'];
    }
    return domInfo as {
        bottom: number;
        height: number;
        left: number;
        right: number;
        scrollHeight: number;
        scrollLeft: number;
        scrollTop: number;
        scrollWidth: number;
        top: number;
        width: number;
    };
};
/**
 *  获取 dom 节点的信息 (异步函数)
 * @param domName dom节点的选择器名称,同css选择器
 * @returns dom节点的相关信息
 * ! 此函数必须promise的写法.在H5端非异步,在微信端它是异步的.
 */
export const getDomInfo = async (domName: string) => {
    let domInfo = await getDomInfoRun(domName);
    if (!domInfo) domInfo = {} as any;
    return domInfo;
};
